﻿
//封装Ajax 跨域使用 多用于获取JSON
function doAjax(data, command, callback) {
    var json = JSON.stringify(data);
    var url = appendServerUrl(command);
    $.ajax({
        url: url,
        type: 'POST',
        data: json,
        dataType: 'JSON',
        beforeSend: function () { loadingShow(); },
        complete: function () { loadingHide(); },
        crossDomain: true,
        success: function (msg) {
            loadingHide();
            if (typeof callback != 'undefined') {
                callback(msg);
                return false;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}
//封装Ajax 本地使用 多用于加载部分视图
function NativeAjax(data, url, callback) {
    $.ajax({
        url: url,
        type: 'POST',
        data: JSON.stringify(data),
        dataType: 'html',
        beforeSend: function () { loadingShow(); },
        complete: function () { loadingHide(); },
        contentType: 'application/json',
        success: function (msg) {
            loadingHide();
            if (callback) {
                callback(msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}
//加载部分视图
//viewResult - 部分视图的HTML
//loadContentId - 填充Id
//loadType - 1：内容替换 2：之后追加
function loadPartialViewResult(viewResult, loadContentId, loadType) {
    var content = $(loadContentId);
    if (loadType == '1') {
        content.html(viewResult);
    } else if (loadType == "2") {
        content.append(viewResult);
    }
}

function loadingShow(message) {
    var strMsg = "loading...";
    if (message !== undefined && message != "") {
        strMsg = message;
    }
    var ajaxbg = $("#LoadingBackground,#LoadingProgressBar");
    if (ajaxbg.html() == null) {
        $("body").append('<div id="LoadingBackground" class="LoadingBackground" ></div><div num="1" id="LoadingProgressBar" class="LoadingProgressBar" >' + strMsg + '</div>');
    } else {
        var num = Number($("#LoadingProgressBar").attr("num"));
        $("#LoadingProgressBar").html(strMsg).attr("num", num + 1);
    }
    $("#LoadingBackground,#LoadingProgressBar").show();
}


function loadingHide() {
    var num = Number($("#LoadingProgressBar").attr("num"));
    if (num > 1) {
        $("#LoadingProgressBar").attr("num", num - 1);
    } else {
        $("#LoadingBackground,#LoadingProgressBar").remove();
    }
}
function SuccessDispose(data) {
    /*
    此方法用于 其他 JS 需要页面加载 完成后 处理的 功能。
    */
}